package com.game.action;

import com.coment.game.fight.Attribute;
import com.coment.game.fight.Attrs;
import com.coment.message.vo.ListEquip;
import com.coment.po.roles.Roles;
import com.coment.vo.RolesListItem;
import com.game.dao.core.GameDao;
import com.game.logic.ranking.GameRakingService;
import com.game.dao.user.roles.RolesDao;
import com.game.logic.roles.RolesService;
import com.xs.action.annotate.ActionController;
import com.xs.action.annotate.ActionMethod;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/// 4-角色操作
@Slf4j
@RestController("/rolesAction")
@ActionController(value = 4, name = "角色操作")
public class RolesActon {
    @Resource
    private RolesService rolesService;
    @Resource
    private RolesDao rolesDao;
    @Resource
    private GameDao gameDao;
    @Resource
    private GameRakingService gameRakingService;


    /**
     * 1.查看拥有的卡片id
     *
     * @param userInfoIds 用户id列表
     * @return 卡片id列表
     */
    @GetMapping("/listOfRolesInfo")
    @ActionMethod(value = 1, name = "查看拥有的卡片id")
    public List<RolesListItem> listOfRolesInfo(Integer[] userInfoIds) {
        return rolesService.getRolesListItems(userInfoIds);
    }

    /**
     * 2.查看角色信息 详细信息
     *
     * @param userInfoId 用户信息id
     * @param cardId     卡片id
     * @return 角色信息
     */
    @GetMapping("/obtainRoleInfo")
    @ActionMethod(value = 2, name = "查看角色信息")
    public Roles obtainRoleInfo(@NotNull Integer userInfoId, @NotNull Integer cardId) {
        return this.rolesDao.searchByUserInfoIdAndCardId(userInfoId, cardId);
    }

    /**
     * 3.尝试升级-需要渡劫
     *
     * @param id 角色id
     * @return 突破后的角色信息
     */
    @GetMapping("/attemptToBreakThrough")
    @ActionMethod(value = 3, name = "尝试突破")
    public Roles attemptToBreakThrough(@NotNull String id) {
        Roles roles = this.rolesService.tryCrossingTheTribulation(id);
        log.debug("尝试突破角色：{}", Attribute.getLogInfo(
                new Attrs(roles.rolePanel.getAttribute()))
        );
        return roles;
    }

    /**
     * 4.查看指定角色战斗力
     *
     * @param rolesId 角色id
     * @return 角色战斗力
     */
    @GetMapping(value = "/obtainRoleFightPower")
    @ActionMethod(value = 4)
    public double obtainRoleFightPower(@NotNull String rolesId) {
        return this.rolesService.obtainRoleFightPower(rolesId);
    }

    /// 5.查看角色体力
    @GetMapping(value = "/obtainRoleStrength")
    @ActionMethod(value = 5)
    public Integer obtainRoleStrength(@NotNull String rolesId) {
        return this.rolesService.obtainRoleStrength(rolesId);
    }

    /// 6.获取玩家拥有的所有角色
    @GetMapping(value = "/list")
    @ActionMethod(value = 6)
    public List<Roles> list(@NotNull Integer userInfoId) {
        return this.rolesDao.searchAllByUserInfoId(userInfoId);
    }

    /// 7. 查看游戏内角色装备
    @GetMapping(value = "/listEquip")
    @ActionMethod(value = 7)
    public List<ListEquip> listEquip(
            @NotNull Integer id,
            int s,
            int t
    ) {
        List<ListEquip> listEquipList = new ArrayList<>();
        GameRakingService.RankingMessage<Integer> integerRankingMessage = gameRakingService.combatEffectiveness(id, s, t);
        Map<Integer, String> userInfoIdSeat = gameDao.findByIdMust(id).userInfoIdSeat;
        integerRankingMessage.rankingList().forEach(integer -> {
            Integer userInfoId = integer.getValue();
            userInfoIdSeat.computeIfPresent(userInfoId, (k, v) -> {

                Map<Integer, Set<Integer>> inventory = this.rolesService.rolesDao.findByIdMust(v).getRolePanel().getCultivatePanel().getInventory();
                ListEquip listEquip = new ListEquip(v, userInfoId, inventory);
                listEquipList.add(listEquip);
                return v;
            });
        });
        return listEquipList;
    }

}
